package jjoms.model.info;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import jjoms.userException.UpdatedHitsInfoException;

import org.springframework.stereotype.Service;

@Service
public class InfoServiceImpl implements InfoService {
	@Resource(name = "infoDAOImpl")
	private InfoDAO infoDAO;
	
	@Override
	public InfoVO findByKeyword(String keyword) throws UpdatedHitsInfoException {
		InfoVO infoVO = infoDAO.sectionFindByKeyword(keyword);
		System.out.println("section" + infoVO);
		if(infoVO == null){
			infoVO = infoDAO.divisionFindByKeyword(keyword);
			System.out.println("division" + infoVO);
		}
		if(infoVO != null){
			throw new UpdatedHitsInfoException(infoVO);			
		}
		return infoVO;
	}
	@Override
	public InfoVO findByKeywordNoException(String keyword) {
		InfoVO infoVO = infoDAO.sectionFindByKeyword(keyword);
		System.out.println("section" + infoVO);
		if(infoVO == null){
			infoVO = infoDAO.divisionFindByKeyword(keyword);
			System.out.println("division" + infoVO);
		}
		return infoVO;
	}
	@Override
	public List<String> getCategoryKind() {
		List<String> list = infoDAO.getCategoryKind();
		if(list==null){
			System.out.println("카테고리배열 빔");
			list = new ArrayList<String>();
		}
		
		return list;
	}

	@Override
	public List<String> getDivisionKind(String category) {
		return infoDAO.getDivisionKind(category);
	}

	@Override
	public void infoRegiseter(InfoVO infoVO) {
		infoDAO.infoRegiseter(infoVO);
	}

	@Override
	public void infoCategory(InfoVO infoVO) {
		  InfoVO ivo = infoDAO.cateforyFindByKeyword(infoVO.getCategory());
	      System.out.println(ivo);
	      infoVO.setCategoryNo(ivo.getCategoryNo());
	      System.out.println("if before : " + infoVO);      
	      if(infoVO.getDivision() == null || infoVO.getDivision().equals("")){
	         infoDAO.insertDivision(infoVO);
	      }else{
	         infoVO.setDivisionNo(infoDAO.divisionFindByKeyword(infoVO.getDivision()).getDivisionNo());
	         System.out.println("insertSection Before : " + infoVO);
	         infoDAO.insertSection(infoVO);
	      }
	}

	@Override
	public void infoUpdateResult(InfoVO infoVO, String beforeKeyword) {
		infoDAO.infoUpdate(infoVO, beforeKeyword);
		infoDAO.categoryUpdate(infoVO, beforeKeyword);
	}
	
	@Override
	public void deleteDivision(String keyword) {
		infoDAO.deleteDivision(keyword);
	}
	
	@Override
	public void deleteSection(String keyword) {
		infoDAO.deleteSection(keyword);
	}

	@Override
	public void infoDelete(String keyword) {
		infoDAO.infoDelete(keyword);
	}

	@Override
	public List<String> getSectionKind(String keyword) {
		return infoDAO.getSectionKind(keyword);
	}

	@Override
	public List<String> getSection(String category) {
	   return infoDAO.getSection(category);
	}

	
}
